<template>
	<!-- 提报使用（部分字段不可编辑） -->
	<div  class="wrapper">
		<!-- <Anchor class="mainAnchor" :labelList="labels" activeId="row-01" labelWidth="80">
			<div class="line" id="row-01"> -->
				<BreakSpace :label="labels[0].title" :containerStyle="{ marginBottom: '10px' }" />
				<infoBase ref="infoBase" :formType="value.examineType" :formOptions="value" v-model="formData" @formchange="infoBaseChange"/>
			<!-- </div>
			<div class="line" id="row-02"> -->
				<BreakSpace :label="labels[1].title" :containerStyle="{ marginBottom: '10px' }" />
				<infoProduct ref="infoProduct" :formType="value.examineType" :formOptions="value" v-model="formData" @formchange="infoProductChange"/>
			<!-- </div>
			<div class="line" id="row-03"> -->
				<BreakSpace :label="labels[2].title" :containerStyle="{ marginBottom: '10px' }" />
				<infoPower ref="infoPower" :formType="value.examineType" :formOptions="value" v-model="formData" @formchange="infoPowerChange"/>
			<!-- </div>
			<div class="line" id="row-04"> -->
				<BreakSpace :label="labels[3].title" :containerStyle="{ marginBottom: '10px' }" />
				<infoCompany ref="infoCompany" :formType="value.type" :formOptions="value" v-model="formData" @formchange="infoCompanyChange"/>
			<!-- </div>
			<div class="line" id="row-05"> -->
				<BreakSpace :label="labels[4].title" :containerStyle="{ marginBottom: '10px' }" />
				<infoStock ref="infoStock" :formType="value.type" :formOptions="value" v-model="formData" @formchange="infoStockChange"/>
			<!-- </div>
			<div class="line" id="row-06"> -->
				<BreakSpace :label="labels[5].title" :containerStyle="{ marginBottom: '10px' }" />
				<infoFinance ref="infoFinance" :formType="value.type" :formOptions="value" v-model="formData" @formchange="infoFinanceChange"/>
			<!-- </div> -->
		<!-- </Anchor> -->
		<infoOpinion ref="infoOpinion" :formType="value.examineType" :formOptions="value" v-model="formData" @formchange="infoOpinionChange"/>
		<div class="dialogBottmView">
			<el-button @click="closeDialog">关 闭</el-button>
			<el-button @click="saveDialog" type="primary">提交</el-button>
		</div>
	</div>
</template>

<script>
	import { authority } from '@/utils/authMixin';
	import { dictionary } from '@/utils/dictMixin';
	import autoLoding from '@/utils/loding';//等待框

	import infoBase from '../dealerFormView/infoBase'; //基本信息
	import infoProduct from '../dealerFormView/infoProduct'; //产品授权
	import infoPower from '../dealerFormView/infoPower'; //授权信息

	import infoCompany from '../dealerForm/infoCompany'; //公司信息
	import infoStock from '../dealerForm/infoStock'; //股东信息
	import infoFinance from '../dealerForm/infoFinance'; //财务信息

	import infoOpinion from '../dealerForm/infoOpinion'; //审核信息

	import { getDetails } from '@/api/cms-api/origin/originInfo';
	import { addAllData, editAllData } from '@/api/cms-api/origin/originInfo/OEM';
	import * as types from '@/api/cms-api/origin/originInfo/types';
	import { isArray } from 'util';
	import vm from './bridge';

	export default {
		mixins: [authority, dictionary, autoLoding],
		components: {
			infoBase,infoProduct,infoPower,infoCompany,infoStock,infoFinance,infoOpinion
		},
		props:{
			value: {
				type: Object,
				required: true
			},
		},
		data() {
			return {
				formData:{},
				labels: [
					{ title: '基本信息', id: 'row-01' },
					{ title: '产品授权', id: 'row-02' },
					{ title: '授权信息', id: 'row-03' },
					{ title: '公司信息', id: 'row-04' },
					{ title: '股东信息', id: 'row-05' },
					{ title: '财务信息', id: 'row-06' }
				],
			};
		},
		mounted(){
			this.loadingVm = this.openLoding('.cover-container');
			if(this.value.type != 'add'){
				this.init();
			}else{
				this.closeLoding(this.loadingVm);
			}
		},
		methods: {
			async init(){
				let res = await getDetails(this.value.id,{});
				if(res.resultCode === 200){
					//转换附件为list<Sting>
					// this.formData = res.data;
					this.formData = this.transformAllEnclosureString(res.data);

					this.$nextTick(() =>{
						//根据状态禁止启用部分的验证
						//vm.$emit('changeDealerType',this.formData.dealerType);
						this.$refs.infoBase.init();
						this.$refs.infoProduct.init();
						this.$refs.infoPower.init();
						//由于base是只读的，所以得用下面的initPre
						// this.$refs.infoCompany.init();
						this.$refs.infoCompany.initPre();
						this.$refs.infoStock.init();
						this.$refs.infoFinance.init();
						this.$refs.infoOpinion.init();
						this.closeLoding(this.loadingVm);
					});
				}
			},
			//确定（验证并提交）
			saveDialog() {
				this.$refs.infoBase.saveDialog();
			},
			//取消操作
			closeDialog() {
				this.$emit('close', { visible: false });
			},
			infoBaseChange(data){
				this.$refs.infoProduct.saveDialog();
			},
			infoProductChange(data){
				this.$refs.infoPower.saveDialog();
			},
			infoPowerChange(data){
				this.$refs.infoCompany.saveDialog();
			},
			infoCompanyChange(data){
				this.$refs.infoStock.saveDialog();
			},
			infoStockChange(data){
				this.$refs.infoFinance.saveDialog();
			},
			infoFinanceChange(data){
				this.$refs.infoOpinion.saveDialog();
			},
			//审核表单
			infoOpinionChange(data){
				//判断‘公司全称’与‘公司名称’是否一致
				if(data.dealerName != data.companyName){
					this.$confirm('【公司全称】与【公司名称】不一致，确认要提交？', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(async() => {
						this.runSave(data);
					}).catch(() => {});
				}else{
					this.runSave(data);
				}
			},
			runSave(data){
				this.loadingVm = this.openLoding('.cover-container');
				let submitData = Object.assign({},data);
				//不再使用由IMG转FILE
				// submitData = this.transformAllEnclosure(submitData);
				//如果是大客户则跳过审核，直接审核通过
				// if(submitData.dealerType === types.DEALER_INFO_DEALER_TYPE_BIG_CUSTOMER){
				// 	//已审核
				// 	submitData.auditStatus = types.DEALER_INFO_STATUS_AUDITED;
				// }else{
					//已提交
					submitData.auditStatus = types.DEALER_INFO_STATUS_SUBMITTED;
				// }
				this.save(submitData);
			},
			//转化表单的附件 转换为list<Map<String,Object>>（提交前使用）
			transformAllEnclosure(data){
				let rdata = Object.assign({},data);

				rdata.designFeeEnclosureList = this.transformEnclosureList(rdata.designFeeEnclosureList);	//设计费附件
				rdata.cashDepositEnclosureList = this.transformEnclosureList(rdata.cashDepositEnclosureList);	//保证金附件

				rdata.businessLicenseEnclosureList = this.transformEnclosureList(rdata.businessLicenseEnclosureList);	//营业执照扫描件
				rdata.legalRepresentativeEnclosureList = this.transformEnclosureList(rdata.legalRepresentativeEnclosureList);	//法定代表人附件
				rdata.firstPaymentEnclosureList = this.transformEnclosureList(rdata.firstPaymentEnclosureList);	//首笔提车款附件

				rdata.openAccountProveEnclosureList = this.transformEnclosureList(rdata.openAccountProveEnclosureList);	//开户证明附件

				rdata.licenceEnclosureList = this.transformEnclosureList(rdata.licenceEnclosureList);	//许可证附件
				return rdata;
			},
			//转化表单的附件 转换为list<Sting>（回显使用）
			transformAllEnclosureString(data){
				let rdata = data;

				rdata.designFeeEnclosureList = this.transformEnclosureStringList(rdata.designFeeEnclosureList);	//设计费附件
				rdata.cashDepositEnclosureList = this.transformEnclosureStringList(rdata.cashDepositEnclosureList);	//保证金附件

				rdata.businessLicenseEnclosureList = this.transformEnclosureStringList(rdata.businessLicenseEnclosureList);	//营业执照扫描件
				rdata.legalRepresentativeEnclosureList = this.transformEnclosureStringList(rdata.legalRepresentativeEnclosureList);	//法定代表人附件
				rdata.firstPaymentEnclosureList = this.transformEnclosureStringList(rdata.firstPaymentEnclosureList);	//首笔提车款附件

				rdata.openAccountProveEnclosureList = this.transformEnclosureStringList(rdata.openAccountProveEnclosureList);	//开户证明附件

				rdata.licenceEnclosureList = this.transformEnclosureStringList(rdata.licenceEnclosureList);	//许可证附件

				//经销商形式
				let dealerAttachAuthList = [];
				if(isArray(rdata.dealerAttachAuthList)){
					rdata.dealerAttachAuthList.forEach(x => {
						dealerAttachAuthList.push(x.financeCompanyId);
					});
				}
				rdata.dealerAttachAuthList = dealerAttachAuthList;

				//车系
				let dealerAttachSeriesList = [];
				if(isArray(rdata.dealerAttachSeriesList)){
					rdata.dealerAttachSeriesList.forEach(x => {
						dealerAttachSeriesList.push(x.seriesId);
					});
				}
				rdata.dealerAttachSeriesList = dealerAttachSeriesList;

				//车型
				let dealerAttachModelList = [];
				if(isArray(rdata.dealerAttachModelList)){
					rdata.dealerAttachModelList.forEach(x => {
						dealerAttachModelList.push(x.modelId);
					});
				}
				rdata.dealerAttachModelList = dealerAttachModelList;
				return rdata;
			},
			//转换成附件对象 转换为list<Map<String,Object>>
			//不再使用由IMG转FILE
			transformEnclosureList(list){
				let fileUrlList = [];
				if(!isArray(list)){
					return fileUrlList;
				}
				// for (let i = 0; i < list.length; i++) {
				// 	let fileUrl = list[i];
				// 	fileUrl = fileUrl.replace(/\\/g,"/");
				// 	fileUrlList.push(Object.assign({},{"name":fileUrl.substring(fileUrl.lastIndexOf('/')+1)},{"url":fileUrl}));
				// }
				// return fileUrlList;
				return list;
			},
			//转换成附件对象(回显) 转换为list<Sting>
			//不再使用由IMG转FILE
			transformEnclosureStringList(list){
				let fileUrlList = [];
				if(!isArray(list)){
					return fileUrlList;
				}
				// for (let i = 0; i < list.length; i++) {
				// 	let fileUrl = list[i];
				// 	fileUrlList.push(fileUrl.fileId);
				// }
				list.forEach(x => {
					fileUrlList.push({name:x.fileName,url:x.fileId});
				});
				return fileUrlList;
			},
			async save(data){
				let res = {};
				if(this.value.type === 'add'){
					res = await addAllData(data);
				}else{
					res = await editAllData(this.value.id,data);
				}
				if(res.resultCode === 200) {
					this.$notify.success({title: '提示信息',message:'提交成功!'});
					this.closeLoding(this.loadingVm);
					this.$emit('close','refresh');
				}else {
					this.closeLoding(this.loadingVm);
					this.$notify.error({title: '提示信息',message:res.errMsg||'提交失败!'});
				}
			},
		},
	};
</script>

<style scoped>
	.wrapper,.line {
		/* height: calc(100vh - (135px + 180px)); */
		height: calc(100vh - 225px);
	}
	.line{
		overflow-x:hidden;
		overflow-y:auto;
	}
</style>